import util from '@/libs/util';
import getDate from '@/util/getDate.js';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
            title: '教师姓名',
                key: 'mainTeacherName'
        }, {
            title: '班级',
                key: 'className'
        }, {
            title: '停课日期',
                key: 'supplyData',
                render: (h, params) => {
                return h('div', {}, getDate.getDate(params.row.supplyData, 'date'))
            }
        }, {title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                return h('div', [
                    h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginRight: '5px'
                        },
                        on: {
                            click: () => {
                            this.isRead = false;
                                this.showEdit(params.row);
                            }
                        }
                    }, '修改'),
                    h('Button', {
                        props: {
                            type: 'error',
                            size: 'small'
                        },
                        on: {
                            click: () => {
                            this.delete(params.row.id);
                            }
                        }
                    }, '删除')
                ]);
                }
            }],
            data: [],
            formData: {},
            teacherList: [],
            classList: [],
            query: {
                name: ''
            },
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            baseuser: JSON.parse(Cookies.get('baseUser')),
            editModal: false,
            isRead: false,

        };
    },
    mounted () {
        this.into();
    },
    methods: {
        into () {
            util.ajax.post('/busine/tescher/info/list', util.toFormData(
                {id: 0}
            )).then(res => {
            this.teacherList = res.data.data.list;
        });

            util.ajax.post('/base/class/list', util.toFormData(
                {id: 0}
            )).then(res => {
                this.classList = res.data.data.list;
        });
            this.loadList();
        },
        loadList () {
            util.ajax.post('/busine/teacher/listSupply', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                name: this.query.name
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        refresh () {
            this.query={
                name: ''
            };
            this.into();
        },
        showAdd () {
            this.isRead = false;
            this.formData = {};
            this.editModal = true;
        },
        showEdit (obj) {
            this.formData = {};
            util.ajax.post('/busine/teacher/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                    this.formData = res.data.data;
                    this.editModal = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/busine/teacher/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                }
            });
        },
        save () {
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            if (!formData.mainTeacherId) {
                this.$Modal.error({content: '负责教师不能为空！'});
                return;
            }
            if (!formData.classId) {
                this.$Modal.error({content: '负责班级不能为空！'});
                return;
            }
            if (!formData.supplyData) {
                this.$Modal.error({content: '停课日期不能为空！'});
                return;
            }
            util.ajax.post('/busine/teacher/saveSupply', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                    this.editModal = false;
                } else {
                    this.$Modal.error({
                        content: res.data.message || '未知错误！'
                    });
                }
            });
        },
    }
}